import java.util.LinkedList;
import java.util.Queue;
import java.util.Stack;

public class BinaryTree {
    int kSize = 0;
    int height;
    int count;
    static class TreeNode{
        char val;
        TreeNode left;
        TreeNode right;

        public TreeNode(char val){
            this.val = val;
        }
        public TreeNode(){}
    }
    public  TreeNode createTree(){
        TreeNode A = new TreeNode('A');
        TreeNode B = new TreeNode('B');
        TreeNode C = new TreeNode('C');
        TreeNode D = new TreeNode('D');
        TreeNode E = new TreeNode('E');
        TreeNode F = new TreeNode('F');
        TreeNode G = new TreeNode('G');
        TreeNode H = new TreeNode('H');
        A.left = B;
        A.right = C;
        B.left = D;
        B.right = E;
        C.left = F;
        C.right = G;
        E.right = H;
        return A;
    }
    void proPrint(TreeNode root){
        if(root == null) {
            return;
        }
        System.out.print(root.val + " ");
        proPrint(root.left);
        proPrint(root.right);
    }
    void midPrint(TreeNode root){
        if(root == null) {
            return;
        }

        midPrint(root.left);
        System.out.print(root.val + " ");
        midPrint(root.right);
    }
    void rePrint(TreeNode root){
        if(root == null) {
            return;
        }
        rePrint(root.left);
        rePrint(root.right);
        System.out.print(root.val + " ");
    }
    //计算节点个数

    //无脑递归法
    public int size(TreeNode root) {
        if(root == null) {
            return count;
        }
        count++;
        size(root.left);
        size(root.right);
        return count;
    }
    //从子问题入手
    public int size1(TreeNode root) {
        if(root == null) {
            return 0;
        }
        count = size1(root.left) + size1(root.right) + 1;
        return count;
    }
    //获得叶子节点个数

    //子问题思路
    public int leaf(TreeNode root) {
        if (root == null) {
            return 0;
        }

        if(root.right == null && root.left == null) {
                return 1;
        }

         count =   leaf(root.left) + leaf(root.right);
        return  count;

    }
    //遍历思路
    public int leaf2(TreeNode root) {
        if(root == null) {
            return 0;
        }
        if (root.left == null & root.right == null ) {
            count++;
        }
        leaf2(root.left);
        leaf2(root.right);

        return count;
    }
    //计算树第k层的节点个数
    public int sumK(TreeNode root , int k) {
        if(root == null) {
            return 0;
        }
        if(k == 1) {
            return 1;
        }
        kSize = sumK(root.left , k - 1) + sumK(root.right,k - 1);
        return kSize;
    }

    //获取树的高度
    //???????????????????
    //为什么局部变量和全局变量结果会不一样

    public int height(TreeNode root){
        if(root == null) {
            return 0;
        }
         int left1 = height(root.left) + 1 ;
         int right1 = height(root.right) + 1 ;
         int max = Math.max(left1,right1) ;
         return max;

    }
    //在二叉树中找对应值,并返回节点
    Boolean a;
    public TreeNode find(TreeNode root,char val) {
         if(root == null) {
             return null;
         }
         if(root.val == val){
             return root;
         }
         TreeNode l =  find(root.left,val);
         if(l != null) {
             return l;
         }
         l = find(root.right , val);
         if(l != null) {
             return l;
         }
         return null;
    }
    //判断两个二叉树是否完全相同
    //时间复杂度Omin(M,N);
    //遇到一个false就不进行过遍历了吗？？？？？？
    public  Boolean identify(TreeNode p1, TreeNode p2){
        if(p1 == null && p2 != null || (p1 != null && p2 == null)){
            return false;
        }

        if(p1 == null && p2 == null) {
            return true;
        }
        if(p1.val != p2.val) {
            return false;
        }
        return  identify(p1.left , p2.left) && identify(p1.right , p2.right);
    }
    //判断是否子树或者相同
    public Boolean isSubtree(TreeNode p1,TreeNode p2){
        if(identify(p1,p2) ){
            return true;
        }
        if(p1 != null && identify(p1.left , p2)){
            return  true;
        }
        if(p1 != null && identify(p1.right , p2)){
            return  true;
        }
        return false;

    }
    //翻转二叉树
    public TreeNode ss(TreeNode root) {
        if(root == null){
            return null;
        }
        if(root.left == null && root.right == null) {
            return root;
        }
        swap(root);
        ss(root.left);
        ss(root.right);
        return root;
    }
    public void swap(TreeNode root){
        TreeNode tmp = root.right ;
        root.right = root.left;
        root.left = tmp;
    }
    //判断是否为平衡二叉树
    //时间复杂度O(n2);
    //常规思路
    public static Boolean isBalanced(TreeNode root){
        if(root == null){
            return true;
        }
        int left = getHeight(root.left);
        int right = getHeight(root.right);
        if(Math.abs(left - right) < 2 && isBalanced(root.right) && isBalanced(root.left)){
            return true;
        }

        return false;
    }

    public static int getHeight(TreeNode root){
        if(root == null) {
            return 0;
        }
        int left = getHeight(root.left) ;
        int right = getHeight(root.right);
        return Math.max(left , right) + 1;
    }
    //高阶思路：在求高度的时候就判断是否满足条件
    public  Boolean isBalance(TreeNode root){

        int result = findHeight(root);
        if(result < 0) {
            return false;
        }
        return true;
    }
    public int findHeight(TreeNode root){
        if( root == null) {
            return 0;
        }
        int left = findHeight(root.left);
        if (left < 0) {
            return -1;
        }
        int right = findHeight(root.right);
        if(left > 0 && right > 0 && Math.abs(left - right) < 2) {
            return Math.max(left,right) + 1;
        }else {
            return -1;
        }
    }
    //判断树是否对称
    public boolean isSymmetric(TreeNode root){
        if( root == null) {
            return  true;
        }
        boolean result = iden(root.left , root.right);
        return result;
    }
    public boolean iden(TreeNode left , TreeNode right) {
        if(left == null && right != null || (left != null && right == null)) {
            return false;
        }
        if(left==null && right == null) {
            return true;
        }
        if(left.val != right.val) {
            return false;
        }
        return iden(left.left,right.right) && iden(left.right,right.left);

    }
    //根据前序遍历得到的字符串创建一棵树，并打印出中序遍历

    int i = 0;
    public TreeNode createTree(String str){
        TreeNode root = null;
        char str1 = str.charAt(i);
        if(str1 != '#'){
            root = new TreeNode(str1);
            i++;
            root.left = createTree(str);
            root.right = createTree(str);
        }else {
            i++;
        }
        return root;
    }
    //层序遍历
    //非递归，用队列
    public void levelOrder(TreeNode root){
        Queue<TreeNode> queue = new LinkedList<>();
        queue.add(root);
        while(!queue.isEmpty()) {
            TreeNode tmp = queue.poll();
            if(tmp.left != null ) {
                queue.offer(tmp.left);
            }
            if (tmp.right != null) {
                queue.offer(tmp.right);
            }
            System.out.print(tmp.val + " ");
        }

    }
    //层序遍历，并且分层
    public void levelOrder1(TreeNode root){
        Queue<TreeNode> queue = new LinkedList<>();

        LinkedList<LinkedList<Character>> bigLinkedList = new LinkedList<>();

        queue.offer(root);
        while(!queue.isEmpty()){
        int size = queue.size();
            LinkedList<Character> linkedList  = new LinkedList<>();
            TreeNode node  = null;
        while(size > 0){
             node = queue.poll();
            linkedList.add(node.val);
            size--;
            if(node.left != null) {
                queue.offer(node.left);
            }
            if (node.right != null){
                queue.offer(node.right);
            }
        }
        bigLinkedList.add(linkedList);
            System.out.println(bigLinkedList);
        }
    }
    //判断二叉树是否为完全二叉树
    public boolean wholeTree(TreeNode root) {
        Queue<TreeNode> queue = new LinkedList<>();
        queue.offer(root);

        while (!queue.isEmpty()) {
            TreeNode node = queue.poll();
            if(node == null){
                while (!queue.isEmpty()) {
                    if(queue.poll() != null) {
                        return false;
                    }else {
                        return true;
                    }
                }
            }
                queue.offer(node.left);
                queue.offer(node.right);
        }
        return true;

    }
    //找节点的公共祖先
    public TreeNode find(TreeNode root ,TreeNode p ,TreeNode q){
        if(root == null ) {
            return null;
        }
        if( root.val == p.val || root.val == q.val) {
            return root;
        }
        TreeNode left = find(root.left,p ,q);
        TreeNode right = find(root.right,p,q);
        if (left != null && right != null) {
            return root;
        }else  if(left != null) {
            return left;
        }else{
            return right;
        }
    }
    //法二：链表交叉法
    public boolean find1(TreeNode root ,TreeNode p , TreeNode q) {
        if(root == null) {
            return false;
        }
        Stack<TreeNode> stack = new Stack<>();
        Stack<TreeNode> stack1 = new Stack<>();
        getPath(root , p ,stack);
        getPath(root , q ,stack1);

        int size1 = stack.size();
        int size2 = stack1.size();

        if(size1 - size2 > 0 ){
            int result = size1 - size2;
            while(result != 0) {
                stack.pop();
                result--;
            }
        }else {
            int result = size2 - size1;
            while(result != 0) {
                stack1.pop();
                result--;
            }
        }
        while(!stack.isEmpty() && !stack1.isEmpty()) {
            if(stack.peek().equals(stack1.peek()) ) {
                return true;
            }
            else {
                stack.pop();
                stack1.pop();
            }
        }
        return false;

    }
    public Boolean getPath(TreeNode root,TreeNode node ,Stack<TreeNode> stack){
        if(root == null) {
            return false;
        }
        stack.push(root);
        if(root.val == node.val){
            return  true;
        }

        boolean left = getPath(root.left , node , stack);
        if(left == true) {
            return true;
        }
        boolean right = getPath(root.right , node ,stack);
        if(right == true) {
            return true;
        }
        stack.pop();

        return false;
    }
    //二叉树转换为字符串
     public String swapString(TreeNode root){
        if(root == null) {
            return null;
        }
        StringBuilder stringBuilder = new StringBuilder();
        func(root , stringBuilder);
        return stringBuilder.toString();
     }
     public void func(TreeNode root, StringBuilder stringBuilder){
        if(root == null) {
            return;
        }
        stringBuilder.append(root.val);

        if(root.left != null) {
            stringBuilder.append("(");
            func(root.left, stringBuilder);
            stringBuilder.append(")");
        }else {
            if (root.left == null){
                stringBuilder.append("()");
            }
            else {
                return;
            }
        }
         if(root.right != null) {
             stringBuilder.append("(");
             func(root.right, stringBuilder);
             stringBuilder.append(")");
         }else {
            return;
         }
     }
     //非递归实现前序遍历
    public void proPrintTree(TreeNode root){
        if(root == null) {
            return;
        }
        Stack<TreeNode> stack = new Stack<>();
        TreeNode cur = root;
        while(cur != null || !stack.isEmpty()) {

            while (cur != null) {
                stack.push(cur);

                cur = cur.left;
            }
            TreeNode top = stack.pop();
            System.out.print(top.val + " ");
            cur = top.right;
        }
        return;
    }
    //后续遍历非递归
    public void rePrintTree(TreeNode root) {
        if (root == null) {
            return;
        }
        Stack<TreeNode> stack = new Stack<>();
        TreeNode cur = root;
        TreeNode preV = null;
        while (cur != null || !stack.isEmpty()) {

            while (cur != null) {
                stack.push(cur);

                cur = cur.left;
            }
            TreeNode top = stack.peek();
            if (top.right == null || top.right == preV) {
                top = stack.pop();
                preV = top;
                System.out.print(top.val + " ");
            } else {
                cur = top.right;
            }
        }
        return;
    }
    //根据前序遍历和中序遍历创建树
    int ia;
    public TreeNode createTree2(char[] arr1 ,char[] arr2, int  s ,int e ){
        if(s > e) {
            return null;
        }

        TreeNode root = new TreeNode(arr1[ia]);
        int findIt = findMiddle(arr2 , s ,e,arr1[ia]);
        ia++;

        root.left = createTree2(arr1 , arr2 , s , findIt - 1);
        root.right = createTree2(arr1 ,  arr2 , findIt + 1 , e );

        return root;
    }
    //中序和后序遍历
    //关键点只需要将root.right和root.left交换顺序即可
    int count1 = 1;
    public TreeNode createTree3(char[] arr1 ,char[] arr2 ,int s ,int e) {

        if(s > e) {
            return null;
        }
        TreeNode root = new TreeNode(arr1[i]);
        int findIt = findMiddle(arr2 , s ,e,arr1[i]);
        i--;


        root.right = createTree2(arr1 ,  arr2 , findIt + 1 , e );
        root.left = createTree2(arr1 , arr2 , s , findIt - 1);

        return root;

    }




    public int findMiddle(char[] arr , int s ,int e,char val) {
        for (int i = s ; i <= e ; i++){
            if (arr[i] == val) {
                return  i;
            }

        }
        return -1;
    }


}
